package com.jp.knowledge.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.JsonObject;
import com.jp.knowledge.activity.JpApplicationWebActivity;
import com.jp.knowledge.adapter.SearchRecommentAdapter;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.my.base.RecycleVAdapter;

/**
 * Created by 528787067@qq.com on 2017/5/11.
 */

public class ProductEvluationRecommendFragment extends ProductEvluationBaseFragment {

    public static ProductEvluationRecommendFragment newInstance(int type, int action){
        ProductEvluationRecommendFragment fragment = new ProductEvluationRecommendFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        bundle.putInt("action", action);
        fragment.setArguments(bundle);
        return fragment;
    }

    private SearchRecommentAdapter adapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new SearchRecommentAdapter(mContext, headLineDatas, 11);
        adapter.addItemClickListener(new RecycleVAdapter.OnItemClickListener() {
            @Override
            public void itemSelect(int position) {
                JpApplicationWebActivity.gotoWebActivity(mContext,
                        "details/recommendDetail?id=" + headLineDatas.get(position).getId() + "&source=kengPo");
            }
        });
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentView = super.onCreateView(inflater, container, savedInstanceState);
        refreshContent.setAdapter(adapter);
        return contentView;
    }

    @Override
    protected void getData(int mode) {
        JsonObject param = new JsonObject();
        if(mode == REFRESH_CODE){
            page = 1;
            param.addProperty("time", System.currentTimeMillis());
            param.addProperty("mode", 0);
        } else{
            page++;
            if(headLineDatas.size() > 0)
                param.addProperty("time", headLineDatas.get(headLineDatas.size()-1).getCreatetime());
            else
                param.addProperty("time", System.currentTimeMillis());
            param.addProperty("mode", 1);
        }
        if(type == 2)
            param.addProperty("page", page);
        param.addProperty("type", type);
        param.addProperty("action", action);
        HttpHelper.getInstance(mContext).recommendList(param, mode, this);
    }

    @Override
    protected void updateAdapter() {
        adapter.updateAdapter(headLineDatas);
    }
}
